import { Component, OnInit, ViewContainerRef } from '@angular/core';
import { NzContextMenuService, NzDropdownMenuComponent } from 'ng-zorro-antd/dropdown';
import { NzModalService } from 'ng-zorro-antd/modal';
import {NzFormatEmitEvent, NzTreeNode } from 'ng-zorro-antd/tree';
import {NzTreeVo} from '../../../../vo/NzTreeVo';
import {AdminService} from "../../../../services/admin.service";
import {AlertService} from "../../../../services/alert.service";
import {RefertypeeditComponent} from "../refertypeedit/refertypeedit.component";
import {ReferdataeditComponent} from "../referdataedit/referdataedit.component";

@Component({
  selector: 'app-refertypelist',
  templateUrl: './refertypelist.component.html',
  styleUrls: ['./refertypelist.component.css']
})
export class RefertypelistComponent implements OnInit {

  tl:Array<NzTreeVo>;
  activatedNode:any;
  searchValue='';
  typeid:string;
  dl:Array<any>;
  constructor(
    private nzContextMenuService: NzContextMenuService
    ,private modal: NzModalService, private viewContainerRef: ViewContainerRef
    ,private deptservice:AdminService
    ,private alertservice:AlertService
  ) {
    this.tl=new Array<NzTreeVo>();
    this.dl=new Array<any>();
  }

  ngOnInit(): void {

    this.getReferTypeList();
  }

  getReferTypeList(){
    this.deptservice.getRefertypes()
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.tl=new Array<NzTreeVo>();
          this.tl=data.list;
        }
        else {
          this.alertservice.showError('获取参考数据异常，请重试');
        }
      })


  }


  onClickNode(data:NzFormatEmitEvent){

    this.activatedNode = data.node!;
    this.getReferDataByTypeId();

  }

  contextMenu($event: MouseEvent, menu: NzDropdownMenuComponent,key:string): void {
    this.nzContextMenuService.create($event, menu);
    this.typeid=key;
  }

  addReferType(){
    const modalwindow= this.modal.create({
      nzTitle: '参考目录信息编辑',
      nzContent: RefertypeeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype:'add'
      },

      nzOnOk:()=>new Promise(resolve => {
        // modalwindow.getContentComponent().save();

        let temp=modalwindow.getContentComponent().t;
        if(!temp.name){
          this.alertservice.showWarning('名称信息必填');
          resolve(false)
        }
        else {
          modalwindow.getContentComponent().save()

          setTimeout(()=>{

            resolve(true);
            this.getReferTypeList();
          }, 2000)
        }


      })
    })
  }


  updateReferType(){
    const modalwindow= this.modal.create({
      nzTitle: '参考目录信息编辑',
      nzContent: RefertypeeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype:'update',
        id:this.typeid
      },

      nzOnOk:()=>new Promise(resolve => {
        // modalwindow.getContentComponent().save();

        let temp=modalwindow.getContentComponent().t;
        if(!temp.name){
          this.alertservice.showWarning('名称信息必填');
          resolve(false)
        }
        else {
          modalwindow.getContentComponent().save()

          setTimeout(()=>{

            resolve(true);
            this.getReferTypeList();
          }, 2000)
        }


      })
    })

  }


  deleteReferType(){
    this.deptservice.deleteRefertypebyid(this.typeid).then((data:any)=>{
        if(data.msg=='ok'){
          this.getReferTypeList();
        }
        else {
          this.alertservice.showError('删除类型异常，请重试');
        }
      })
  }

  getReferDataByTypeId(){
    this.deptservice.getReferdatasByTypeid(this.activatedNode.key)
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.dl=new Array<any>();
          this.dl=data.list;

        }
        else if(data.msg=='empty'){
          this.dl=new Array<any>();
        }
        else {
          this.alertservice.showError('获取参考数据异常，请重试');
        }
      })
  }


  addReferData(){
    const modalwindow= this.modal.create({
      nzTitle: '参考数据信息编辑',
      nzContent: ReferdataeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype:'add',
        typeid:this.activatedNode.key
      },

      nzOnOk:()=>new Promise(resolve => {

        let temp=modalwindow.getContentComponent().d;
        if(!temp.name){
          this.alertservice.showWarning('名称不能为空，请输入');
          resolve(false);
        }
        else {
          modalwindow.getContentComponent().save();
          setTimeout(()=>{

            resolve(true);
            this.getReferDataByTypeId();
          }, 2000)

        }


      })
    })
  }


  updateReferData(referdata:string){
    const modalwindow= this.modal.create({
      nzTitle: '参考数据信息编辑',
      nzContent: ReferdataeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype:'update',
        typeid:this.activatedNode.key,
        id:referdata
      },

      nzOnOk:()=>new Promise(resolve => {

        let temp=modalwindow.getContentComponent().d;
        if(!temp.name){
          this.alertservice.showWarning('名称不能为空，请输入');
          resolve(false);
        }
        else {
          modalwindow.getContentComponent().save();
          setTimeout(()=>{

            resolve(true);
            this.getReferDataByTypeId();
          }, 2000)

        }


      })
    })
  }

  deleteReferData(referdataid:string){
    this.deptservice.deleteReferdatabyid(referdataid)
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.getReferDataByTypeId();
        }
        else {
          this.alertservice.showError('删除参考数据异常，请重试');
        }
      })
  }


/*  sendReferDateToCache(){
    this.deptservice.sendReferDateToCache()
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.getReferTypeList();
        }
      })
  }*/
}
